/**
 * 品牌页直间工 PPH 表格组件
 */
<script setup>
import { ref, watch, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import http from '@/axios/http'

const route = useRoute()
const brand = ref(route.query.brand)

const factory = ref('')
const year = ref('')
const month = ref('')
const model = ref('')
const operation = ref('')

const factoryList = ref([])
const yearList = ref([])
const monthList = ref([])
const modelList = ref([])
const operationList = ref([])

const tableData = ref([])
const isLoading = ref(false)

watch([factory, year, month, model, operation], () => {
  getDirectPPH()
})

function getDirectOption() {
  http.post("/brand/getDirectOption", {
    brand: brand.value
  }).then(res => {
    factoryList.value = res.data.factoryList
    yearList.value = res.data.yearList
    monthList.value = res.data.monthList
    modelList.value = res.data.modelList
    operationList.value = res.data.operationList
  }).catch(err => {
    console.log(err)
  })
}

async function getDirectPPH() {
  isLoading.value = true

  await http.post("/brand/getDirectPPH", {
    brand: brand.value,
    factory: factory.value,
    year: year.value,
    month: month.value,
    model: model.value,
    operation: operation.value
  }).then(res => {
    tableData.value = res.data
    if (tableData.value.length === 100) {
      // eslint-disable-next-line no-undef
      ElMessage({
        message: '表格中最多只能显示 100 条数据！',
        type: 'warning',
        duration: 5000
      })
    }
  }).catch(err => {
    console.log(err)
  })

  isLoading.value = false
}

onMounted(() => {
  getDirectOption()
  getDirectPPH()
})
</script>

<template>
  <div id="brand-directPPH-table">
    <div class="directPPH-top">
      <div class="directPPH-title">
        <span>直工POH</span>
      </div>
      
      <div class="directPPH-selector">
        <div class="directPPH-selector-item">
          <span>工厂：</span>

          <el-select
            v-model="factory"
            clearable
            filterable
            :teleported="false"
            no-data-text="暂无数据"
            no-match-text="暂无该工厂"
            placeholder="请选择工厂"
            style="display: inline-block; width: 115px;"
          >
            <div style="max-height: 170px;">
              <el-option
                v-for="item in factoryList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </div>
            
          </el-select>
        </div>

        <div class="directPPH-selector-item">
          <span>型体：</span>

          <el-select
            v-model="model"
            clearable
            filterable
            :teleported="false"
            no-data-text="暂无数据"
            no-match-text="暂无该型体"
            placeholder="请选择型体"
            style="display: inline-block; width: 150px;"
          >
            <div style="max-height: 170px;">
              <el-option
                v-for="item in modelList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </div>
          </el-select>
        </div>

        <div class="directPPH-selector-item">
          <span>制程：</span>

          <el-select
            v-model="operation"
            clearable
            filterable
            :teleported="false"
            no-data-text="暂无数据"
            no-match-text="暂无该制程"
            placeholder="请选择制程"
            style="display: inline-block; width: 170px;"
          > 
            <div style="max-height: 170px;">
              <el-option
                v-for="item in operationList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </div>
          </el-select>
        </div>

        <div class="directPPH-selector-item">
          <span>年份：</span>

          <el-select
            v-model="year"
            clearable
            filterable
            :teleported="false"
            no-data-text="暂无数据"
            no-match-text="暂无该年份"
            placeholder="请选择年份"
            style="display: inline-block; width: 115px;"
          >
            <div style="max-height: 170px;">
              <el-option
                v-for="item in yearList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </div>
          </el-select>
        </div>

        <div class="directPPH-selector-item">
          <span>月份：</span>

          <el-select
            v-model="month"
            clearable
            filterable
            :teleported="false"
            no-data-text="暂无数据"
            no-match-text="暂无该月份"
            placeholder="请选择月份"
            style="display: inline-block; width: 115px;"
          >
            <div style="max-height: 170px;">
              <el-option
                v-for="item in monthList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </div>
          </el-select>
        </div>
      </div>
    </div>

    <div class="directPPH-bottom">
      <el-table
        :data="tableData"
        v-loading="isLoading"
        element-loading-background="rgba(50, 50, 80, 0.8)"
        empty-text="暂无数据"
        max-height="24vh"
        size="small"
      >
        <el-table-column label="工厂" width="70" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.factory }}
          </template>
        </el-table-column>

        <el-table-column label="年份" width="80" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.year }}
          </template>
        </el-table-column>

        <el-table-column label="月份" width="70" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.month }}
          </template>
        </el-table-column>

        <el-table-column label="型体" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.model }}
          </template>
        </el-table-column>

        <el-table-column label="制程" width="200" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.operation }}
          </template>
        </el-table-column>

        <el-table-column label="达成率" width="80" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.rate !== null ? (scope.row.rate !== 0 ? scope.row.rate + '%' : '\\') : '\\' }}
          </template>
        </el-table-column>

        <el-table-column label="直工PPH" width="100" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.zg_pph !== null ? (scope.row.zg_pph !== 0 ? scope.row.zg_pph.toFixed(2) : '\\') : '\\' }}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
#brand-directPPH-table {
  width: 100%;
  height: 100%;
  overflow: auto;

  .directPPH-top {
    width: 100%;
    height: 30%;
    color: white;
    font-size: 1.8vh;
    font-weight: normal;
    position: relative;

    .directPPH-title {
      display: inline-block;
      margin-top: 1%;
      margin-left: 2.8%;
    }

    .directPPH-selector {
      width: 85%;
      position: absolute;
      left: 13%;
      top: 5%;
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      align-items: center;

      .directPPH-selector-item {
        margin-left: 2.5%;
        margin-bottom: 1%;

        --el-color-primary: green;

        &:deep(.el-input__wrapper) {
          background-color: black;
        }

        &:deep(.el-input__inner) {
          color: #ccc;
        }

        &:deep(.el-select__popper.el-popper) {
          background-color: black;
        }

        &:deep(.el-select-dropdown__item) {
          color: white;
          background-color: black;
        }

        &:deep(.el-select-dropdown__item.selected) {
          color: white;
          background-color: green;
        }

        &:deep(.el-select-dropdown__item.hover) {
          color: white;
          background-color: #666;
        }
      }
    }
  }

  .directPPH-bottom {
    width: 98%;
    height: 63%;
    margin: 0 1% 0 1%;

    --el-bg-color: rgb(50, 50, 120);
    --el-text-color-secondary: white;

    .el-table {
      --el-table-text-color: white;
      --el-table-bg-color: rgb(50, 50, 80);
      --el-table-tr-bg-color: rgb(50, 50, 80);
      --el-table-row-hover-bg-color: rgb(80, 80, 80);
      --el-table-border-color: transparent;

      border-radius: 1vh;
      font-size: 1.6vh;
    }
  }
}
</style>